package com.lzy.system.dictManage.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.conf.JSONResult;
import com.lzy.system.dictManage.dto.SysDicGroupDto;
import com.lzy.system.dictManage.dto.convert.SysDicGroupConvert;
import com.lzy.system.dictManage.entity.SysDicGroup;
import com.lzy.system.dictManage.service.ISysDicGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统字典分组 前端控制器
 * </p>
 *
 * @author lcc
 * @since 2023-12-31
 */
@RestController
@RequestMapping("/dictManage/sys-dic-group")
@Api(tags = "系统字典分组")
@Validated
public class SysDicGroupController {

    private ISysDicGroupService iSysDicGroupService;
    private SysDicGroupConvert sysDicGroupConvert;

    @Autowired
    public SysDicGroupController(ISysDicGroupService iSysDicGroupService, SysDicGroupConvert sysDicGroupConvert) {
        this.iSysDicGroupService = iSysDicGroupService;
        this.sysDicGroupConvert = sysDicGroupConvert;
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ApiOperation("获取字典分组列表")
    public JSONResult getList(
            @ApiParam("字典标题") @RequestParam(name = "title",required = false) String title,
            @ApiParam("每页显示行数") @RequestParam(name = "limit", required = true, defaultValue = "10") int limit,
            @ApiParam("当前页数") @RequestParam(name = "page", required = true, defaultValue = "1") int page
    ) {
        QueryWrapper<SysDicGroup> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(title)) {
            queryWrapper.like("title", title);
        }
        IPage<SysDicGroup> page_obj = iSysDicGroupService.page(new Page<SysDicGroup>(page, limit), queryWrapper);
        return JSONResult.ok(page_obj);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加字典分组")
    public JSONResult add(
            @ApiParam("字典分组对象") @RequestBody @Validated SysDicGroupDto sysDicGroupAddDto
    ) {
        SysDicGroup sysDicGroup = sysDicGroupConvert.addDtoToEntity(sysDicGroupAddDto);
        iSysDicGroupService.save(sysDicGroup);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("更新字典分组")
    public JSONResult update(
            @ApiParam("字典分组对象") @RequestBody @Validated SysDicGroupDto sysDicGroupAddDto
    ) {
        SysDicGroup sysDicGroup = sysDicGroupConvert.addDtoToEntity(sysDicGroupAddDto);
        iSysDicGroupService.updateById(sysDicGroup);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation("删除字典分组")
    public JSONResult delete(
            @ApiParam("ids") @RequestBody @Validated List<String> ids
    ) {
        iSysDicGroupService.delete(ids);
        return JSONResult.ok();
    }
}
